package com.CuiChangHe.studentmanager.service.studentsimpleinfo.impl;

import com.CuiChangHe.studentmanager.dao.StudentSimpleInfoDao;
import com.CuiChangHe.studentmanager.entity.studentsimpleinfo.StudentSimpleInfoData;
import com.CuiChangHe.studentmanager.entity.studentsimpleinfo.StudentSimpleInfoReturnMsg;
import com.CuiChangHe.studentmanager.mapper.StudentSimpleInfoMapper;
import com.CuiChangHe.studentmanager.service.studentsimpleinfo.StudentSimpleInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * @Description :学生基本信息业务实现
 * @Author :崔昌赫
 * @Date : 2021/11/16
 */
@Service
@Slf4j
public class StudentSimpleInfoServiceImpl implements StudentSimpleInfoService {

    private StudentSimpleInfoMapper mapper;

    @Autowired
    public void setMapper(StudentSimpleInfoMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    @Transactional(readOnly = true)
    public StudentSimpleInfoReturnMsg getStudentList() {
        log.info("开始查找数据库所有信息.");
        StudentSimpleInfoDao[] studentInfo = mapper.getAllStudentInfo();
        log.info("查找结果为:{}", Arrays.toString(studentInfo));
        StudentSimpleInfoReturnMsg returnMsg = new StudentSimpleInfoReturnMsg();
        returnMsg.setCode(20000);
        returnMsg.setData(new StudentSimpleInfoData(studentInfo.length, studentInfo));
        return returnMsg;
    }

    @Override
    @Transactional(readOnly = true)
    public StudentSimpleInfoReturnMsg getStudentInfoByStudentId(String studentId) {
        log.info("开始通过学号查找学生信息.学号:{}",studentId);
        StudentSimpleInfoDao[] studentInfoByStudentId = mapper.getStudentInfoByStudentId(studentId);
        log.info("查找结果为:{}", Arrays.toString(studentInfoByStudentId));
        StudentSimpleInfoReturnMsg returnMsg = new StudentSimpleInfoReturnMsg();
        returnMsg.setCode(20000);
        returnMsg.setData(new StudentSimpleInfoData(1,studentInfoByStudentId));
        return returnMsg;
    }

    @Override
    @Transactional
    public StudentSimpleInfoReturnMsg updateStudentInfoByStudentId(String studentId, StudentSimpleInfoDao data) {
        StudentSimpleInfoReturnMsg returnMsg = new StudentSimpleInfoReturnMsg();
        log.info("开始通过学号更新学生信息.学号:{},data:{}",studentId,data);
        log.info("通过学号查找用学生信息.学号:{}",studentId);
        StudentSimpleInfoDao[] studentInfoByStudentId = mapper.getStudentInfoByStudentId(studentId);
        if(studentInfoByStudentId.length==0){
            log.warn("未能通过学号查找到学生信息!");
            returnMsg.setCode(50000);
            returnMsg.setMessage("未能通过学号查找到学生信息!");
            return returnMsg;
        }
        if(data.getStudentid().equals("")){
            log.info("检测到data中studentid为空,保持旧数据.data.studentid:{},旧数据:{}",data.getStudentid(),studentInfoByStudentId[0].getStudentid());
            data.setStudentid(studentInfoByStudentId[0].getStudentid());
        }
        if(data.getName().equals("")){
            log.info("检测到data中name为空,保持旧数据.data.name:{},旧数据:{}",data.getName(),studentInfoByStudentId[0].getName());
            data.setName(studentInfoByStudentId[0].getName());
        }
        if(data.getCollege().equals("")){
            log.info("检测到data中的college为空,保持旧数据.data.college:{},旧数据:{}",data.getCollege(),studentInfoByStudentId[0].getCollege());
            data.setCollege(studentInfoByStudentId[0].getCollege());
        }
        if(data.getClasses().equals("")){
            log.info("检测到data中的classes为空,保持旧数据.data.classes:{},旧数据:{}",data.getClasses(),studentInfoByStudentId[0].getClasses());
            data.setClasses(studentInfoByStudentId[0].getClasses());
        }
        if(data.getStatus().equals("")){
            log.info("检测到data中的status为空,保持旧数据.data.status:{},旧数据:{}",data.getStatus(),studentInfoByStudentId[0].getStatus());
            data.setStatus(studentInfoByStudentId[0].getStatus());
        }
        if(data.getBirthday().equals("")){
            log.info("检测到data中的birthday为空,保持旧数据.birthday:{},旧数据:{}",data.getBirthday(),studentInfoByStudentId[0].getBirthday());
            data.setBirthday(studentInfoByStudentId[0].getBirthday());
        }
        log.info("数据整理完成，准备上库.");
        Boolean aBoolean = mapper.updateStudentInfoByStudentId(studentId, data);
        if(!aBoolean){
            log.warn("数据未能正确上库!studentid:{},data:{}",studentId,data);
            returnMsg.setCode(50000);
            returnMsg.setMessage("数据提交失败!");
            return returnMsg;
        }
        log.info("上库成功.本次业务结束.");
        returnMsg.setCode(20000);
        returnMsg.setMessage("修改成功!");
        return returnMsg;
    }

    @Override
    @Transactional
    public StudentSimpleInfoReturnMsg addNewStudentSimpleInfo(StudentSimpleInfoDao data) {
        StudentSimpleInfoReturnMsg returnMsg = new StudentSimpleInfoReturnMsg();
        log.info("开始创建新学生信息.data:{}", data);
        StudentSimpleInfoDao[] studentInfoByStudentId = mapper.getStudentInfoByStudentId(data.getStudentid());
        if (studentInfoByStudentId.length!=0) {
            log.warn("通过学号查找到学生信息!data:{},info:{}", data, studentInfoByStudentId);
            returnMsg.setCode(50000);
            returnMsg.setMessage("该学生信息已存在!");
            return returnMsg;
        }
        log.info("开始增加到数据库.data:{}", data);
        Boolean aBoolean = mapper.addNewStudentSimpleInfo(data);
        if (!aBoolean) {
            log.warn("数据增加到数据库失败!data:{}", data);
            returnMsg.setCode(50000);
            returnMsg.setMessage("数据增加到数据库失败!");
            return returnMsg;
        }
        log.info("数据增加到数据库成功!本次业务结束.");
        returnMsg.setCode(20000);
        returnMsg.setMessage("增加学生信息成功!");
        return returnMsg;
    }

    @Override
    @Transactional
    public StudentSimpleInfoReturnMsg deleteStudentSimpleInfo(String studentid) {
        StudentSimpleInfoReturnMsg returnMsg = new StudentSimpleInfoReturnMsg();
        log.info("开始通过学号删除学生信息.studentid:{}",studentid);
        StudentSimpleInfoDao[] studentInfoByStudentId = mapper.getStudentInfoByStudentId(studentid);
        if (studentInfoByStudentId.length==0) {
            log.warn("未通过学号查找到学生信息!studentid:{}",studentid);
            returnMsg.setCode(50000);
            returnMsg.setMessage("未通过学号查找到学生信息!");
            return returnMsg;
        }
        log.info("通过学号查找到学生信息.即将开始删除:data:{}",studentInfoByStudentId[0]);
        Boolean aBoolean = mapper.deleteStudentInfoByStudentId(studentid);
        if (!aBoolean){
            log.warn("删除学生失败!无法从数据库内删除该学生数据!data:{}",studentInfoByStudentId[0]);
            returnMsg.setCode(50000);
            returnMsg.setMessage("删除学生失败!无法从数据库内删除该学生数据!");
            return returnMsg;
        }
        log.info("删除学生信息成功!本次业务结束.");
        returnMsg.setCode(20000);
        returnMsg.setMessage("删除学生信息成功!");
        return returnMsg;
    }


}
